import React, { Component } from 'react'
import './personal.scss'

import { DatePicker, List } from 'antd-mobile';
import Axios from 'axios';

const nowTimeStamp = Date.now();
let minDate = new Date(0);
const maxDate = new Date(nowTimeStamp + 1e7);
if (minDate.getDate() !== maxDate.getDate()) {
  minDate = new Date(maxDate.getFullYear(), maxDate.getMonth(), maxDate.getDate());
}

export default class Personal extends Component {
  constructor(){
    super()
    this.state={
      sex:'男',
      date:'',
      age:''
    }
  }
  render() {
    this.data = JSON.parse(localStorage.getItem('loginId'))
    return (
      <div className='myPersonal'>
        <div className='myTou'>
          <img src='//yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png' alt=''></img>
        </div>
        <div className='myMessageBox'>
          <div className='myMessage border-bottom'><p>昵称</p><p>{this.data.username}</p></div>
          <div className='myMessage border-bottom'><p>账号</p><p>{this.data.tel}</p></div>
          <div className='myMessage border-bottom'><p>性别</p><p><span className='mySex'><input type='radio'
          checked={this.state.sex === '男' ? true:false}
          onChange={(e) =>{if(!e.target.checked){ this.setState({sex:'女'})}else{this.setState({sex:'男'})}
          }}/>男</span>
          <span><input type='radio' checked={this.state.sex === '女' ? true:false}
          onChange={(e) =>{if(!e.target.checked){
            this.setState({sex:'男'})}else{this.setState({sex:'女'})}
          }}/>女</span></p></div>
          <div className='myMessage border-bottom'>
          <DatePicker
          className="myListBox"
            mode="date"
            value={this.state.date}
            onChange={date => this.setState({ date })}
            extra={this.state.age}
          >
          <List.Item arrow="horizontal" className='myList'>出生日期</List.Item>
          
        </DatePicker>
          </div>
        </div>
        <div className='mySet'>
          <p className='border-top' onClick={()=>{this.props.history.go(-1)}}>取消</p>
          <p onClick={() =>{
            Axios.post('http://10.36.149.139:9000/api/user/setdata',{
              tel:this.data.tel,
              sex:this.state.sex,
              age:this.state.date === '' ?  '' :this.state.date
            }).then((data) =>{
              this.props.history.go(-1)
            })
          }}>保存</p>
        </div>
      </div>
    )
  }

  componentDidMount(){
   Axios.post('http://10.36.149.139:9000/api/user/findmy',{
      tel:this.data.tel
    }).then(({data}) =>{
      let time= new Date(data.result.age).getTime()
      time = new Date(time)
      let year = time.getFullYear()
      let month = time.getMonth()+1
      let date = time.getDate()
      this.setState({
        sex:data.result.sex,
        age:year+'-'+month+'-'+date
      })
    })
  }
}
